package com.liuqi.openai.core.client;

import com.liuqi.openai.util.StringUtil;
import java.time.Duration;

/**
 * openai client 提供基础信息
 *
 * @author liuqi
 * @date 2025/7/18
 **/
public abstract class AbstractOpenAiClient implements OpenAiClient {

    /**
     * openai api 接口地址
     */
    protected final String baseUrl;

    /**
     * openai api 版本
     */
    protected final String apiVersion;

    /**
     * openai api apikey
     */
    protected final String openAiApiKey;

    protected Duration connectTimeout = Duration.ofSeconds(60);
    protected Duration readTimeout = Duration.ofSeconds(180);

    protected AbstractOpenAiClient(String baseUrl, String apiVersion, String openAiApiKey) {
        if (StringUtil.isEmpty(baseUrl)) {
            throw new IllegalArgumentException("baseUrl cannot be null or empty");
        }
        if (StringUtil.isEmpty(openAiApiKey)) {
            throw new IllegalArgumentException("openAiApiKey cannot be null or empty");
        }
        this.baseUrl = baseUrl.endsWith("/") ? baseUrl : baseUrl + "/";
        this.apiVersion = apiVersion;
        this.openAiApiKey = openAiApiKey;
    }

    protected String formatUrl(String endpoint) {
        return baseUrl + endpoint + apiVersionQueryParam();
    }

    protected String apiVersionQueryParam() {
        if (apiVersion == null || apiVersion.trim().isEmpty()) {
            return "";
        }
        return "?api-version=" + apiVersion;
    }

    protected AbstractOpenAiClient(String baseUrl, String apiVersion, String openAiApiKey, Duration connectTimeout, Duration readTimeout) {
        this(baseUrl, apiVersion, openAiApiKey);
        if (connectTimeout == null) {
            throw new IllegalArgumentException("connectTimeout cannot be null");
        }
        if (readTimeout == null) {
            throw new IllegalArgumentException("readTimeout cannot be null");
        }
        this.connectTimeout = connectTimeout;
        this.readTimeout = readTimeout;
    }

    public abstract static class Builder<T extends OpenAiClient, B extends Builder<T, B>> {

        /**
         * openai api 接口地址
         */
        public String baseUrl;

        /**
         * openai api 版本
         */
        public String apiVersion;

        /**
         * openai api apikey
         */
        public String openAiApiKey;

        public Duration connectTimeout = Duration.ofSeconds(60);
        public Duration readTimeout = Duration.ofSeconds(180);

        public B baseUrl(String baseUrl) {
            this.baseUrl = baseUrl;
            return (B) this;
        }

        public B apiVersion(String apiVersion) {
            this.apiVersion = apiVersion;
            return (B) this;
        }

        public B openAiApiKey(String openAiApiKey) {
            this.openAiApiKey = openAiApiKey;
            return (B) this;
        }

        public B connectTimeout(Duration connectTimeout) {
            this.connectTimeout = connectTimeout;
            return (B) this;
        }

        public B readTimeout(Duration readTimeout) {
            this.readTimeout = readTimeout;
            return (B) this;
        }

        public abstract T build();

    }

}
